<template>
  <XtxBread>
    <XtxBreadItem to="/">首页</XtxBreadItem>
    <XtxBreadItem v-if="category.top" :to="`/category/${category.top.id}`">{{category.top.name}}</XtxBreadItem>
    <transition name="fade-righ" mode="out-in">
      <XtxBreadItem :key="category.sub.id" v-if="category.sub">{{category.sub.name}}</XtxBreadItem>
    </transition>
  </XtxBread>
</template>
<script>
import { useRoute } from 'vue-router'
import { useStore } from 'vuex'
import { computed } from 'vue'
export default {
  name: 'SubBread',
  setup() {
    const route = useRoute()
    const store = useStore()
    const category = computed(() => {
      const cate = {}
      store.state.category.list.forEach(top => {
        if (top.children) {
          const sub = top.children.find(sub => {
            return sub.id === route.params.id
          })
          if (sub) {
            cate.top = { id: top.id, name: top.name }
            cate.sub = { id: sub.id, name: sub.name }
          }
        }
      })
      return cate
    })

    return {
      category
    }
  }
}
</script>

<style lang="less" scoped>

// 面包屑动画
.fade-righ-enter-from,
.fade-righ-leave-to {
  transform: translateX(20px);
  opacity: 0;
}

.fade-righ-enter-active,
.fade-righ-leave-active {
  transition: all 0.5s;
}

.fade-righ-enter-to,
.fade-righ-leave-from {
  transform: none;
  opacity: 1;
}

</style>